package com.zhang.zhangapp.view.custom;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhang.zhangapp.R;
import com.zhang.zhangapp.utils.DensityUtils;
import com.zhang.zhangapp.utils.ScreenUtils;

/**
 * Created by 罗肖 on 2018/4/18.
 */

public class SearchView extends LinearLayout {

    private String[] labelArr;
    private Context context;

    private View.OnClickListener listener;

    int padding = 15;

    public SearchView(Context context, String[] labelArr, View.OnClickListener listener) {
        super(context);
        this.labelArr = labelArr;
        this.context = context;
        this.listener = listener;
        initView();
    }

    private void initView() {
        if (labelArr != null) {
            int screentWidth = ScreenUtils.getScreenWidth(context);
            int childWidth = 0;
            int px = DensityUtils.dp2px(context, padding);
            for (String labelName : labelArr) {
                TextView textView = new TextView(context);
                textView.setText(labelName);
                textView.setBackgroundResource(R.drawable.z_gray_bg_f3);
                textView.setPadding(px, 10, px, 10);
                textView.setOnClickListener(listener);
                int width = textView.getMeasuredWidth();
                childWidth += width;
                if ((childWidth + px * 2) > screentWidth) {
                    childWidth = 0;
                }
                if (childWidth == 0) {
                    LinearLayout layout = new LinearLayout(context);
                    layout.setOrientation(LinearLayout.HORIZONTAL);
                    layout.setPadding(px, 10, px, 10);
                    this.addView(layout);
                }
                ((LinearLayout) this.getChildAt(this.getChildCount() - 1)).addView(textView);
            }
        }
    }
}
